package team.bluepen.supermarket.data.dao;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import team.bluepen.supermarket.constant.Constant;
import team.bluepen.supermarket.constant.Role;
import team.bluepen.supermarket.data.dao.abstraction.MarketDatabase;
import team.bluepen.supermarket.data.entity.MarketTable;
import team.bluepen.supermarket.data.entity.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * 操作{@link User}对应的表
 * @author Kai
 */
public class UserDatabase extends MarketDatabase<User> {
    private static final String TAG = "UserDatabase";
    private static final MarketTable USER_TABLE = new MarketTable(Constant.UserTable.TABLE_NAME,
            new ArrayList<>(Collections.singletonList(Constant.UserTable.TABLE_FAMILY)));

    public UserDatabase() throws IOException {
        super(USER_TABLE);
    }

    public static MarketTable getUserTable() {
        return USER_TABLE;
    }

    @Override
    protected Put buildPut(User user) {
        Put put = new Put(Bytes.toBytes(user.getRow()));
        put.addColumn(Bytes.toBytes(Constant.UserTable.TABLE_FAMILY), Bytes.toBytes(Constant.UserTable.QUALIFIER_USERNAME),
                Bytes.toBytes(user.getUsername() == null ? "" : user.getUsername()));
        put.addColumn(Bytes.toBytes(Constant.UserTable.TABLE_FAMILY), Bytes.toBytes(Constant.UserTable.QUALIFIER_PASSWORD),
                Bytes.toBytes(user.getPassword() == null ? "" : user.getPassword()));
        put.addColumn(Bytes.toBytes(Constant.UserTable.TABLE_FAMILY), Bytes.toBytes(Constant.UserTable.QUALIFIER_ID),
                Bytes.toBytes(user.getId()));
        put.addColumn(Bytes.toBytes(Constant.UserTable.TABLE_FAMILY), Bytes.toBytes(Constant.UserTable.QUALIFIER_ROLE),
                Bytes.toBytes(user.getRole() == null ? "GUEST" : user.getRole().name()));
        // 若不指定角色则默认为Guest身份
        return put;
    }

    @Override
    protected User getInformation(Result result) {
        User user = new User();
        for (Cell cell : result.rawCells()) {
            String row = Bytes.toString(CellUtil.cloneRow(cell));
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            convertHelper(user, row, qualifier, value);
        }
        return user;
    }

    private void convertHelper(User user, String row, String qualifier, String value){
        if (user == null){
            // 必须传入不为空的User对象
            return;
        }
        // 转换为实体类
        user.setRow(row);
        if (Constant.UserTable.QUALIFIER_USERNAME.equals(qualifier)) {
            user.setUsername(value);
        } else if (Constant.UserTable.QUALIFIER_PASSWORD.equals(qualifier)) {
            user.setPassword(value);
        } else if (Constant.UserTable.QUALIFIER_ROLE.equals(qualifier)){
            user.setRole(Role.get(value));
        } else if (Constant.UserTable.QUALIFIER_ID.equals(qualifier)){
            user.setId(Bytes.toLong(Bytes.toBytes(value)));
        }
    }
}
